package org.itboys.seller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.manager.SellerMemberService;
import org.itboys.manager.SellerWeixinService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.AceUtils;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.seller.entity.Seller;
import org.itboys.seller.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * 商家
 * @author weisky
 *
 */
@RestController 
@RequestMapping("/seller") 
public class SellerController extends BaseController{

	@Autowired
	private SellerService sellerService;
	@Autowired
	private SellerMemberService sellerMemberService;
	@Autowired
	private SellerWeixinService sellerWeixinService;
	
	/**
	 * 跳到商家管理页面 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toListVM")
	public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/seller/seller-list");
	}
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		String name = (String) param.get("name");
		if(StringUtils.isNotBlank(name)){//根据商家名称  模糊查询
			param.put("name", AceUtils.getPattern(name));
		}
		Page<Seller> page = sellerWeixinService.sellerListByPage(param, request);
		JsonPageUtils.renderJsonPage(page.getTotalPages(), page.getData(), response);
	}
	
	/**
	 * 返回所有的店铺列表
	 * @param response
	 * @param request
	 */
	@RequestMapping("/allList")
	public void allList(HttpServletResponse response,
			HttpServletRequest request) {
		final Map<String, Object> sqlMap = QueryParamUtils
				.builderQueryMap(request);
		List<Seller> list = sellerService.list(sqlMap);
		AjaxUtils.renderJson(response, list);
	}
	
	/**
	 * 新增或者修改
	 * @param seller
	 * @param logos:商家logo
	 * @param mainImgs：主图
	 * @param request
	 * @param response
	 */
	@RequestMapping("/save")
	public void save(Seller seller, 
			@RequestParam(required=false) MultipartFile logos, 
			@RequestParam(required=false) MultipartFile mainImgs,
			HttpServletRequest request, HttpServletResponse response){
		boolean result = sellerMemberService.addSellerAndCreateMember(seller, logos, mainImgs);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/**
	 * 修改商家的状态
	 * @param sellerId
	 * @param state
	 * @param response
	 */
	@RequestMapping("/updateState")
	public void updateState(Long sellerId, int state, HttpServletResponse response){
		boolean result = sellerService.updateState(sellerId, state);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/**
	 * 获取一条数据
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getById")
	public void getById(Long id, HttpServletRequest request, HttpServletResponse response){
		AjaxUtils.renderJson(response, sellerService.getById(id));
	}
	
	/**
	 * 删除一条数据
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delete")
	public void delete(Long id, HttpServletRequest request, HttpServletResponse response){
		boolean result = sellerService.dele(id);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	
}
